<template>
  <div class="dashboard-container">
    <div class="container">
      <div class="tableBar">
        <el-input
          v-model="queryParamsName"
          placeholder="请输入套餐名称"
          style="width: 250px"
          clearable
        >
          <template #prefix>
            <el-icon style="cursor: pointer" @click="handleQuery">
              <Search />
            </el-icon>
          </template>
        </el-input>
        <div class="tableLab">
          <span class="span-btn delBut" @click="handleDel()">批量删除</span>
          <span class="span-btn editBut" @click="handleStatus(1)"
            >批量启售</span
          >
          <span
            style="border: none"
            class="span-btn delBut"
            @click="handleStatus(0)"
            >批量停售</span
          >
          <el-button type="primary" @click="handleAdd()">+ 新建套餐</el-button>
        </div>
      </div>
      <el-table
        :data="tableData"
        stripe
        class="tableBox"
        @selection-change="handleSelectionChange"
        ref="OnTable"
      >
        <el-table-column type="selection" width="25"></el-table-column>
        <el-table-column prop="name" label="套餐名称"></el-table-column>
        <el-table-column prop="image" label="图片" align="center">
          <template #default="scope">
            <!-- <el-image :src="require('@/assets/dish-images/'+ scope.row.image)" :preview-src-list="[ require('@/assets/dish-images/'+ scope.row.image) ]" :preview-teleported="true"> -->
            <!-- <el-image
              :src="require('@/assets/images/noImg.png')"
              :preview-src-list="[require('@/assets/images/noImg.png')]"
              :preview-teleported="true"
            >
              <template #error>
                <img src="@/assets/images/noImg.png" />
              </template>
            </el-image> -->
            <el-image
              v-if="scope.row && scope.row.image"
              :src="scope.row.image"
              :preview-teleported="true"
            >
              <template #error>
                <img src="@/assets/images/noImg.png" />
              </template>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column prop="categoryName" label="套餐分类"></el-table-column>
        <el-table-column prop="price" label="售价"></el-table-column>
        <el-table-column label="售卖状态">
          <template #default="scope">
            <span style="margin-right: 10px">{{
              scope.row.status == "0" ? "启售" : "停售"
            }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="updateTime" label="最后操作时间">
        </el-table-column>
        <el-table-column label="操作" width="260" align="center">
          <template #default="scope">
            <el-button
              size="small"
              class="editBut"
              @click="handleEdit(scope.row)"
              >修改</el-button
            >
            <el-button
              size="small"
              class="editBut"
              @click="handleStatus(scope.row.status ? 0 : 1, scope.row)"
              >{{ scope.row.status == "0" ? "启售" : "停售" }}</el-button
            >
            <el-button size="small" class="delBut" @click="handleDel(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="pageList"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        v-model:current-page.sync="page"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      ></el-pagination>
    </div>

    <el-dialog v-model="open" :title="dialogTitle" width="80%" destroy-on-close>
      <setmeal-add :id="id" @close="close" @getSetmealList="getSetmealList" />
    </el-dialog>
  </div>
</template>

<script setup>
// import { Search } from "@element-plus/icons-vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { ref, reactive } from "vue";
import { getList, updateStatusById,delSetmeal } from "@/api/setmeal/setmeal";
import SetmealAdd from "./SetmealAdd.vue";

const queryParamsName = ref("");
const total = ref(0);
const page = ref(1);
const pageSize = ref(10);
const tableData = ref([]);

const open = ref(false);
const dialogTitle = ref();
const id = ref(""); //修改id
const idList = ref([]);

const getSetmealList = () => {
  const params = {
    page: page.value,
    pageSize: pageSize.value,
    name: queryParamsName.value,
  };
  getList(params).then((res) => {
    res.data.records = res.data.records.map((obj) => {
        obj.price = (obj.price / 100).toFixed(2);
        return obj;
      });
    tableData.value = res.data.records;
    total.value = res.data.total;
  });
};
getSetmealList();

/* 查询按钮 */
const handleQuery = () => {
  getSetmealList();
  ElMessage({ type: "success", message: "查询成功" });
};

/* 添加按钮 */
const handleAdd = () => {
  id.value = "";
  open.value = true;
  dialogTitle.value = "添加套餐";
};

/* 修改按钮 */
const handleEdit = (row) => {
  id.value = row.id;
  open.value = true;
  dialogTitle.value = "修改套餐";
};

/* 删除按钮 */
const handleDel = (row) => {
  //批量删除
  if (!row) {
    if (idList.value.length == 0) {
      return ElMessage({ type: "error", message: "请选择删除对象！" });
    }
  } else {
    idList.value.push(row.id);
  }
  ElMessageBox.confirm("确认删除该套餐, 是否继续?", "确定删除", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
  })
    .then(() => {
      delSetmeal(idList.value)
        .then((res) => {
          if(res.code==1){ 
          ElMessage({ type: "success", message:res.data });
          idList.value = [];
          getSetmealList();
        }else if(res.code==0){
          ElMessage({ type: "error", message: res.msg });
        }
         
        })
        .catch(() => {
          idList.value = [];
        });
    })
    .catch(() => {
      idList.value = [];
    });
};

/* 启售、停售按钮 */
const handleStatus = (status, row) => {
  //批量
  if (!row) {
    if (idList.value.length == 0) {
      return ElMessage({ type: "error", message: "请选择操作套餐！" });
    }
  } else {
    idList.value.push(row.id);
  }
  ElMessageBox.confirm("确认更改该套餐状态?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      updateStatusById({ status: status, idList: idList.value })
        .then((res) => {
          if (res.code == 1) {
            ElMessage({ type: "success", message: res.data });
            idList.value = [];
            getSetmealList();
          } else {
            ElMessage({ type: "error", message: res.msg });
          }
        })
        .catch(() => {
          idList.value = [];
        });
    })
    .catch(() => {
      idList.value = [];
    });
};

/* 弹框关闭 */
const close = () => {
  open.value = false;
};

/* 表格选择 */
const handleSelectionChange = (val) => {
  let checkArr = [];
  val.forEach((item) => {
    checkArr.push(item.id);
  });
  idList.value = checkArr;
};

/* 修改页码按钮 */
const handleCurrentChange = (val) => {
  page.value = val;
  getSetmealList();
};

/* 修改每页条目数按钮 */
const handleSizeChange = (val) => {
  pageSize.value = val;
  getSetmealList();
};
</script>

<style lang="scss" scoped>
.el-image {
  vertical-align: middle;
  max-width: 50px;
  max-height: 50px;
  width: 40px;
  height: 40px;
}
</style>

